/**
 * 
 */
package auction;

/**
 * Represents a Bid to an auction
 * @author Gernot
 *
 */
public class Bid {
    private String user;
    private double amount;
    private boolean groupBid;
    private long timestamp;

    private int id; //only required for Group-Bids, that are not yet confirmed
    private String confirm1 = null;
    private String confirm2 = null;
    
    private GroupBidTimeout timeout;
    
    public GroupBidTimeout getTimeout() {
        return timeout;
    }

    public void setTimeout(GroupBidTimeout timeout) {
        this.timeout = timeout;
    }

    /**
     * Generate a normal bid
     * @param user
     * @param amount
     */
    public Bid(String user, double amount, long timestamp) {
        super();
        this.user = user;
        this.amount = amount;
        this.groupBid=false;
        this.id = -1;
        this.timestamp = timestamp;
    }
    
   /**
    * Creates a GroupBid
    * @param user
    * @param amount
    * @param id
    */
    public Bid(String user, double amount, int id){
        this.user=user;
        this.amount = amount;
        this.groupBid = true;
        this.id = id;
    }
    
    /**
     * @param username
     * @return
     */
    public boolean isFromThisUser(String username){
        return user.equals(username);
    }
    
    /**
     * @param other
     * @return
     */
    public boolean isFromSameUser(Bid other){
        return this.user.equals(other.user);
    }
    
    /**
     * @param other
     * @return
     */
    public boolean isHigher(Bid other){
        return this.amount>other.amount;
    }

    public void overbidden(AccountManager am, Bid other, String auctionname, int id){
        am.notifyUser(this.user, "!new-bid "+auctionname+"\n");
        am.reportBidEvent("BID_OVERBID", user, id, amount);
    }
    
    public String getUser() {
        return this.user;
    }
    
    public double getAmount() {
        return amount;
    }
    
    public boolean isGroupBid() {
        return groupBid;
    }
    
    public boolean isThisGroupBid(String usr, double amount, int id){
        return this.user.equals(usr) && this.amount ==amount && this.id == id;
    }
    
    /**
     * confirms this bid. returns if is second confirm
     * @param user
     * @return
     */
    public boolean confirm(String confirmer){
        if (this.confirm1==null){
            this.confirm1 = confirmer;
            return false;
        } else if (this.confirm2==null){
            this.confirm2 = confirmer;
            return true;
        } else {
            return false;
        }
        
    }
    
    
    public String getConfirm1() {
        return confirm1;
    }

    public String getConfirm2() {
        return confirm2;
    }

    /**
     * Returns if both bids have same user and id
     * @param other
     * @return
     */
    public boolean isSameAuctionUser(Bid other){
        return this.user.equals(other.user) && this.id == other.id;
    }

    @Override
    public String toString() {
        return this.user+ " "+ this.id + " "+this.amount;
    }

	/**
	 * @param timestamp the timestamp to set
	 * @return The bid.
	 */
	public Bid setTimestamp(long timestamp) {
		this.timestamp = timestamp;
		return this;
	}

	/**
	 * @return the timestamp
	 */
	public long getTimestamp() {
		return timestamp;
	}
    
    
}
